﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyPhongKham
{
    public partial class FrmlistPatient : Form
    {
        string strconnect = "Data Source=CALVINVU\\SQLSERVER;Initial Catalog=QLPK;Persist Security Info=True;User ID=sa;Password=87kvs@07";
            SqlConnection conn = null;
            SqlDataAdapter daBenhNhan = null;
            DataTable dtBenhNhan = null;
            /*SqlDataAdapter daPhai = null;
            DataTable dtPhai = null;*/
            bool Them;

        public FrmlistPatient()
        {
            InitializeComponent();
        }

        private void FrmlistPatient_Load(object sender, EventArgs e)
        {
            LoadData();
            // TODO: This line of code loads data into the 'qLPKDataSet.Khachhang' table. You can move, or remove it, as needed.
            //this.khachhangTableAdapter.Fill(this.qLPKDataSet.Khachhang);

        }
        void LoadData()
        {
            try
            {
                conn = new SqlConnection(strconnect);
                daBenhNhan = new SqlDataAdapter("select * from khachhang",conn);
                dtBenhNhan = new DataTable();
                dtBenhNhan.Clear();
                daBenhNhan.Fill(dtBenhNhan);
                dgvBenhnhan.DataSource = dtBenhNhan;

                //this.txtMabn.ResetText();
                this.txtTenbn.ResetText();
                this.txtDiachi.ResetText();
                this.txtDienthoai.ResetText();
                this.txtNamsinh.ResetText();

                this.btnLuu.Enabled = false;
                this.btnhuybo.Enabled = false;
                this.panel.Enabled = false;

                this.btnThem.Enabled = true;
                this.btnsua.Enabled = true;
                this.btnxoa.Enabled = true;
                this.btntrove.Enabled = true;
            }
            catch (SqlException)
            {
                MessageBox.Show("Không lấy được nội dung trong table Benh Nhan. Lỗi rồi!!!");
            }
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
        
            LoadData();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Them = true;
            //this.txtMabn.ResetText();
            this.txtTenbn.ResetText();
            this.txtDiachi.ResetText();
            this.txtDienthoai.ResetText();
            this.txtNamsinh.ResetText();
            
            this.btnLuu.Enabled = true;
            this.btnhuybo.Enabled = true;
            this.panel.Enabled = true;

            this.btnThem.Enabled = false;
            this.btnsua.Enabled = false;
            this.btnxoa.Enabled = false;
            this.btntrove.Enabled = false;

            //this.txtMabn.Focus();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            Them = false;
            this.panel.Enabled = true;
            int r = dgvBenhnhan.CurrentCell.RowIndex;
            //this.txtMabn.Text = dgvBenhnhan.Rows[r].Cells[0].Value.ToString();
            this.txtTenbn.Text = dgvBenhnhan.Rows[r].Cells[1].Value.ToString();
            //this.cbPhai.SelectedItem = dgvBenhnhan.Rows[r].Cells[5].Value.ToString();
            if (dgvBenhnhan.Rows[r].Cells[5].Value.ToString() == "Nam")
            {
                rbtNam.Checked = true;
            }
            else
            {
                rbtNu.Checked = true;
            }
            this.txtNamsinh.Text = dgvBenhnhan.Rows[r].Cells[4].Value.ToString();
            this.txtDiachi.Text = dgvBenhnhan.Rows[r].Cells[2].Value.ToString();
            this.txtDienthoai.Text = dgvBenhnhan.Rows[r].Cells[3].Value.ToString();

            this.btnLuu.Enabled = true;
            this.btnhuybo.Enabled = true;
            this.panel.Enabled = true;

            this.btnThem.Enabled = false;
            this.btnsua.Enabled = false;
            this.btnxoa.Enabled = false;
            this.btntrove.Enabled = false;

           // this.txtMabn.Focus();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            string strPhai = "";
            if (rbtNam.Checked == true)
            {
                strPhai = "Nam";
            }
            else
            {
                strPhai = "Nữ";
            }
            conn.Open();
            if (Them)
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("SET IDENTITY_INSERT Khachhang ON");
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = System.String.Concat("insert into Khachhang (TenKH,phai,Namsinh,DiaChi,DienThoai) values(" + "N'"
                    //+ this.txtMabn.Text.ToString() + "','"
                    + this.txtTenbn.Text.ToString() + "',N'"
                    + strPhai + "',N'" // this.cbPhai.SelectedItem.ToString()
                    + this.txtNamsinh.Text.ToString() + "',N'"
                    + this.txtDiachi.Text.ToString() + "',N'" 
                    + this.txtDienthoai.Text.ToString() + "')");
                    cmd.ExecuteNonQuery();
                    LoadData();
                    MessageBox.Show("Đã thêm xong!", "Thông Báo");
                }
                catch (SqlException)
                {
                    MessageBox.Show("Không thêm được.Lỗi rồi!");
                }
            }
            if (!Them)
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("SET IDENTITY_INSERT Khachhang ON");
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;

                    int r = dgvBenhnhan.CurrentCell.RowIndex;
                    string strMabn = dgvBenhnhan.Rows[r].Cells[0].Value.ToString();
                    cmd.CommandText = System.String.Concat("Update Khachhang set TenKH=N'" + this.txtTenbn.Text.ToString() + " ', Phai=N'" + strPhai + "', NamSinh='" + this.txtNamsinh.Text.ToString() + " ', Diachi=N'" + this.txtDiachi.Text.ToString() + "',Dienthoai=N'" + this.txtDienthoai.Text.ToString() + "' where MaKH='" + strMabn + "'");//this.cbPhai.SelectedItem.ToString()
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    LoadData();
                    MessageBox.Show("Đã sửa xong","Thông Báo");
                }

                catch (SqlException)
                {
                    MessageBox.Show("Không thêm được. Lỗi rồi");   
                }
               
            }
            conn.Close();
        }

        private void btnhuybo_Click(object sender, EventArgs e)
        {
            //this.txtMabn.ResetText();
            this.txtTenbn.ResetText();
            this.txtDiachi.ResetText();
            this.txtDienthoai.ResetText();
            this.txtNamsinh.ResetText();

            this.btnThem.Enabled = true;
            this.btnsua.Enabled = true;
            this.btnxoa.Enabled = true;
            this.btntrove.Enabled = true;

            this.btnLuu.Enabled = false;
            this.btnhuybo.Enabled = false;
            this.panel.Enabled = false;
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            conn.Open();
            try
            {
                DialogResult traloi;
                traloi = MessageBox.Show("Bạn có chắc chắn xóa không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (traloi == DialogResult.OK)
                {
                    SqlCommand cmd = new SqlCommand("SET IDENTITY_INSERT Khachhang ON");
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    int r = dgvBenhnhan.CurrentCell.RowIndex;
                    string strMabn = dgvBenhnhan.Rows[r].Cells[0].Value.ToString();

                    cmd.CommandText = System.String.Concat("Delete from Khachhang where makh=" + strMabn + "");
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    LoadData();
                    //MessageBox.Show("Đã xóa xong!", "Thông Báo");
                }
            }
            catch (SqlException)
            {
                MessageBox.Show("Không xóa được. Lỗi rồi");   
            }
            conn.Close();
        }

        private void btntrove_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch(keyData)
            {                
                case (Keys.Control | Keys.R):
                    btnReload_Click(null, null);
                    break;
                case (Keys.Control | Keys.N):
                    btnThem_Click(null, null);
                    break;
                case (Keys.Control | Keys.H):
                    btnhuybo_Click(null, null);
                    break;
                case (Keys.Control | Keys.S):
                    btnLuu_Click(null, null);
                    break;
                case (Keys.Control | Keys.E):
                    btnsua_Click(null, null);
                    break;
                case (Keys.Control | Keys.X):
                    btnxoa_Click(null, null);
                    break;
                case (Keys.Control | Keys.T):
                    btntrove_Click(null, null);
                    break;
            }
 
            return base.ProcessCmdKey(ref msg, keyData);
        }

        
    }
}
